package com.srwl.mytx.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.domain.Orders;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.List;

public class OfferRecordAdapter extends RecyclerView.Adapter<OfferRecordAdapter.MyViewHolder> {
    private static final String TAG = "DonationRecordAdapter";
    private static final int ITEM_TYPE_NO_DATA = 0;
    private static final int ITEM_TYPE_NORMAL = 1;
    private final List<Orders> ordersList;
    private final Context context;


    public OfferRecordAdapter(Context context, List<Orders> ordersList) {
        this.ordersList = ordersList;
        this.context = context;

    }

//    @Override
//    public int getItemViewType(int position) {
//
//        return ordersList.get(position).getType();
//
//    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_NO_DATA) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, parent, false));
        }
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_offer_record_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (ordersList.size() == 0) {
            return;
        }
        final Orders orders = ordersList.get(position);
        User targetUser = orders.getUser();
        if (targetUser != null) {
            //Glide.with(context).load(targetUser.getAvatar()).diskCacheStrategy(DiskCacheStrategy.ALL).error(R.drawable.tx_default_avatar).into(holder.iv_avatar);
            GlideLoadUtils.glideLoad(context,targetUser.getAvatar(),holder.iv_avatar,R.drawable.tx_default_avatar_1);
            holder.tv_name.setText(targetUser.getUsername());
        }
        holder.tv_offerCount.setText("第" + orders.getOfferCount() + "次");
        holder.tv_amount.setText(Utils.moneyFormat(orders.getAmount()));
//        if (orders.getStatus()!=null&&orders.getStatus() == Constant.ORDER_STATUS_OFFER_SUCCESS) {
//            holder.tv_auctionSuccess.setText("[成功]");
//            holder.tv_auctionSuccess.setVisibility(View.VISIBLE);
//        } else {
//            holder.tv_auctionSuccess.setVisibility(View.INVISIBLE);
//        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {

        if (ordersList.size() == 0) {
            return 1;
        }
        return ordersList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (ordersList.size() == 0) {
            return ITEM_TYPE_NO_DATA;
        }
        return ITEM_TYPE_NORMAL;


    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_avatar;
        TextView tv_name;
        TextView tv_offerCount;
        TextView tv_amount;
        TextView tv_auctionSuccess;


        public MyViewHolder(View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_offerCount = itemView.findViewById(R.id.tv_offer_count);
            tv_amount = itemView.findViewById(R.id.tv_amount);
           // tv_auctionSuccess = itemView.findViewById(R.id.tv_auction_success);

        }
    }


}